<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>高阶概念 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/aggs-high-level.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/aggs-high-level.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/aggs-high-level.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/aggs-high-level.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-node">高阶概念</span>
</div>
<div class="navheader">
<span class="prev">
<a href="aggregations.html">« 聚合</a>
</span>
<span class="next">
<a href="_buckets.html">桶 »</a>
</span>
</div>
<div class="chapter">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="aggs-high-level"></a>高阶概念<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/301_Aggregation_Overview.asciidoc">edit</a>
</h2>
</div></div></div>
<p>类似于 DSL 查询表达式，聚合也有 <em>可组合</em> 的语法：独立单元的功能可以被混合起来提供你需要的自定义行为。这意味着只需要学习很少的基本概念，就可以得到几乎无尽的组合。</p>
<p>要掌握聚合，你只需要明白两个主要的概念：</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<em>桶（Buckets）</em>
</span>
</dt>
<dd>
满足某一条件的文档的集合
</dd>
<dt>
<span class="term">
<em>指标（Metrics）</em>
</span>
</dt>
<dd>
桶内文档的统计信息
</dd>
</dl>
</div>
<p>这就是全部了！每个聚合都是一个或者多个桶和零个或者多个指标的组合。翻译成粗略的SQL语句来解释吧：</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT COUNT(color) <a id="CO183-1"></a><i class="conum" data-value="1"></i>
FROM table
GROUP BY color <a id="CO183-2"></a><i class="conum" data-value="2"></i></pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO183-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">COUNT(color)</code> 相当于指标(metric)。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO183-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">GROUP BY color</code> 相当于桶(bucket)。</p>
</td>
</tr>
</table>
</div>
<p>桶在概念上类似于 SQL 的分组（GROUP BY），而指标则类似于 <code class="literal">COUNT()</code> 、 <code class="literal">SUM()</code> 、 <code class="literal">MAX()</code> 等统计方法。</p>
<p>让我们深入这两个概念 并且了解和这两个概念相关的东西。</p>



</div>
<div class="navfooter">
<span class="prev">
<a href="aggregations.html">« 聚合</a>
</span>
<span class="next">
<a href="_buckets.html">桶 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>